<?php
include_once('config/config.php');

$admin = LoginCheck();

if ($_POST) {
  $name = isset($_POST['name']) ? trim($_POST['name']) : '';

  if (empty($name)) {
    notice('姓名不能为空');
  }

  if (empty($name)) {
    notice('姓名不能为空');
  }

  $result  = find("SELECT `name` FROM `pre_department` WHERE `name` = '$name'");
  if ($result) {
    notice('部门已存在');
  }

  // 封装数据
  $data = [
    'name' => $name,
  ];

  // 数据库添加部门
  $addresult = add("department", $data);

  if ($addresult['code'] === 1) {
    notice('部门添加成功', 'departmentList.php');
  } else {
    notice('部门添加失败');
  }
}

// 查询部门数据
$DepSql = "SELECT * FROM `pre_department` ORDER BY `id` DESC";

$DepData = all($DepSql);

// 获取部门数据里的第一个部门
$depid = $DepData[0]['id'] ?? 0;

$JobSql = "SELECT * FROM `pre_job` WHERE `depid` = '$depid' ORDER BY `id` DESC";

$JObData = all($JobSql);

?>



<!DOCTYPE html>
<html lang="en">

<head>
  <?php include_once('common/meta.php'); ?>
  <link rel="stylesheet" href="/assets/lib/city-picker/city-picker.css">

  <script src="/assets/lib/city-picker/city-picker.data.min.js"></script>
  <script src="/assets/lib/city-picker/city-picker.min.js"></script>
</head>
<style>
  #upload {
    width: 200px;
    height: 200px;
    object-fit: cover;
  }
</style>

<body>
  <?php include_once('common/header.php'); ?>

  <?php include_once('common/menu.php'); ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">添加部门</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">Home</a> <span class="divider">/</span></li>
      <li class="active">departmentAdd</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">

        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='PersonList.php'"><i class="icon-list"></i>
            部门列表
          </button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="post" enctype="multipart/form-data">
                <label>部门名称</label>
                <input type="text" name="name" class="input-xxlarge"><br>
                <input class="btn btn-primary" type="submit" value="添加部门" />
              </form>
            </div>
          </div>
        </div>

        <?php include_once('common/footer.php'); ?>
      </div>
    </div>
  </div>

</body>

</html>




<script>
  $('#depid').change(function() {
    let depid = $(this).val();

    $.ajax({
      type: 'post',
      url: 'api.php',
      data: {
        depid,
        action: 'job'
      },
      dataType: 'json',
      success: (res) => {

        let option = '';

        if (res.code === 1) {
          for (let item of res.data) {
            option += `<option value="${item.id}">${item.name}</option>`;
          }
        } else {
          option = `<option value="0">暂无职位</option>`
        }

        $('#jobid').html(option);

      }
    })
  })
</script>